<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\UsersBillsSourcesModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\usersBillsSources;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\UsersBillsSourcesForm;

class UsersBillsSourcesController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $source_model = new UsersBillsSourcesModel($em);

        $sources = $source_model->findAll();
        return $this->render('BillingBundle:UsersBillsSources:index.html.twig', array('sources' => $sources));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $source_model = new UsersBillsSourcesModel($em);
        $source = new usersBillsSources();
        //$source_model->create($source->initDefaultEntity());
        //return new RedirectResponse($this->generateUrl('sources_show'));

        $dir=opendir(dirName('../'.basename(__FILE__)).'/vendor/mpanel/classes/PaySystems/');
        $file_string = array();
        while ($file = readdir($dir))
        {
            if (in_array($file,array('.','..')))
                continue;
            $expl=explode('.', $file);
            $expl_size=sizeof($expl);
            if (mb_strtolower($expl[$expl_size-1],'UTF-8') != 'php')
                continue;
            $file_len = sizeof($file_string);
            $file_string[$file_len] = $file;
        }
        closedir($dir);
        array_multisort($file_string);

        $form = $this->createForm(new UsersBillsSourcesForm($file_string), $source);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $source->setFile($file_string[$source->getFile()]);
                $source_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('10');//страница платёжных систем
                $log->setObjectId($source->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание платёжной системы
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('bills_sources_show'));
            }
        }
        return $this->render('BillingBundle:UsersBillsSources:create.html.twig', array('form' => $form->createView()));
    }
    public function updateAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $source_model = new UsersBillsSourcesModel($em);
        $source = $source_model->findByPK($id);

        $dir=opendir(dirName('../'.basename(__FILE__)).'/vendor/mpanel/classes/PaySystems/');
        $file_string = array();
        
        while ($file = readdir($dir))
        {
            if (in_array($file,array('.','..')))
                continue;
            $expl=explode('.', $file);
            $expl_size=sizeof($expl);
            if (mb_strtolower($expl[$expl_size-1],'UTF-8') != 'php')
                continue;
            $file_len = sizeof($file_string);
            $file_string[$file_len] = $file;
            //if ($source->getFile() == $file)
                //$index = count($file_string);
        }
        closedir($dir);
        array_multisort($file_string);
        
        foreach($file_string as $key => $file)
        {
            if ($source->getFile() == $file)//чтобы автоматически выбирался файл в select
            {
                $source->setFile($key);
                break;
            }
        }
        $form = $this->createForm(new UsersBillsSourcesForm($file_string), $source);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $source->setFile($file_string[$source->getFile()]);
                $source_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('10');//страница платёжных систем
                $log->setObjectId($source->getId());//изменяемый объект
                $log->setActionTypeId('2');//изменение платёжной системы
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('bills_sources_show'));
            }
        }
        return $this->render('BillingBundle:UsersBillsSources:create.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $source_model = new UsersBillsSourcesModel($em);
        $source_model->removeByPK($id);

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('10');//страница платёжных систем
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление платёжной системы
        $log_model->create($log);
        return new RedirectResponse($this->generateUrl('bills_sources_show'));
    }
}